package com.fangrui.controller;

import com.fangrui.pojo.Emp;
import com.fangrui.pojo.Result;
import com.fangrui.service.EmpService;
import com.fangrui.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

//Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/26 17:24
@Slf4j
@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping
    public Result login(@RequestBody Emp emp) {
        log.info("员工登录:{}", emp);
        Emp res = empService.login(emp);
        if(res == null){
            return Result.error("登陆失败,用户名或密码错误！");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("emp",res.toString());
        String jwt = JwtUtils.generateJwt(map);
        return Result.success(jwt);
    }
}
